package cn.ad.server.domain.ad;

import cn.ad.server.domain.enumration.AdFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.joda.time.DateTime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class Strategy implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -770741153445643149L;

	static final String VERSION = "0";
	static final String CACHE_NAME = "strategy";
	private long id;
	private String name;
	private String description;
	private Campaign campaign;
	private int campaignId;
	private int priority;
	private Date startTime;
	private Date endTime;
	private int maxImpressions;
	private int dayImpressions;
	private int maxClicks;
	private int dayClicks;
	private BigDecimal budget;
	private BigDecimal dayBudget;
	private int speedMode;
	private int speedBase;
	private Date createTime;
	private Date updateTime;
	private int status;
	private int buyMode;
	private BigDecimal unitPrice;
	private int feeType;

	private List<Interest> interestTargert;

	private StrategyCrowdMap crowdTargert;
	private StrategyDeviceMap deviceTargert;

	private DayTargert dayTargert;

	private List<Integer> geoCountryTargert;
	private List<Integer> geoStateTargert;
	private List<Integer> geoCityTargert;
	private List<Integer> geoDistrictTargert;
	private List<Creative> creatives;
	private List<AdFormat> formatTypeList;
	private StrategyGroup strategyGroup;
	private int strategyGroupId;

	/**
	 * 指定的创意列表。
	 */
	private List<Long> specifyCreativeIds;
	/**
	 * 临时匹配的的创意。
	 */
	private List<Creative> matchCreatives;

	public static String cacheKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(VERSION).append(":").append(id).toString();
	}

	public String toCacheKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(VERSION).append(":").append(this.getId()).toString();
	}

	@Deprecated
	public String maxImpressionsKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.impressions")
				.toString();
	}

	public static String maxImpressionsKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.impressions").toString();
	}

	@Deprecated
	public long maxImpressionsExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	public static long maxImpressionsExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	@Deprecated
	public String dayImpressionsKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.impressions").toString();
	}

	public static String dayImpressionsKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.impressions").toString();
	}

	@Deprecated
	public String maxClicksKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.clicks")
				.toString();
	}

	public static String maxClicksKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.clicks").toString();
	}

	@Deprecated
	public long maxClicksExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	public static long maxClicksExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	@Deprecated
	public String dayClicksKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.clicks").toString();
	}

	public static String dayClicksKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.clicks").toString();
	}

	@Deprecated
	public String maxBudgetKey() {
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append("max.budget")
				.toString();
	}

	public static String maxBudgetKey(long id) {
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append("max.budget").toString();
	}

	@Deprecated
	public long maxBudgetExpireHour() {
		if (this.getEndTime() == null)
			return 0;
		long dif = this.getEndTime().getTime() - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	public static long maxBudgetExpireHour(long endTimeSec) {
		if (endTimeSec == 0)
			return 0;
		long dif = endTimeSec - System.currentTimeMillis();
		return dif / 1000 / 3600 + 1;
	}

	@Deprecated
	public String dayBudgetKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	public static String dayBudgetKey(long id) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("day.budget").toString();
	}

	@Deprecated
	public String timeBucketKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "";
		if (this.getSpeedBase() == 0 || this.getSpeedBase() == 10) {
			base = "click";
		} else if (this.getSpeedBase() == 1 || this.getSpeedBase() == 11) {
			base = "impressions";
		} else if (this.getSpeedBase() == 2 || this.getSpeedBase() == 12) {
			if (this.getFeeType() == 0) {
				base = "budget.cpc";
			} else {
				base = "budget.cpm";
			}
		}

		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("time.bucket").append(":").append(base).toString();
	}

	public static String timeBucketKey(long id, int speedBase, int feeType) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "";
		if (speedBase == 0 || speedBase == 10) {
			base = "click";
		} else if (speedBase == 1 || speedBase == 11) {
			base = "impressions";
		} else if (speedBase == 2 || speedBase == 12) {
			if (feeType == 0) {
				base = "budget.cpc";
			} else {
				base = "budget.cpm";
			}
		}

		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("time.bucket").append(":").append(base).toString();
	}

	@Deprecated
	public String latestConsumeKey() {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "";
		if (this.getSpeedBase() == 0) {
			base = "click";
		} else if (this.getSpeedBase() == 1) {
			base = "impressions";
		} else if (this.getSpeedBase() == 2) {
			if (this.getFeeType() == 0) {
				base = "budget.cpc";
			} else {
				base = "budget.cpm";
			}
		}

		return new StringBuilder(CACHE_NAME).append(":").append(this.getId()).append(":").append(date).append(":")
				.append("latest.consume").append(":").append(base).toString();
	}

	public static String latestConsumeKey(long id, int speedBase, int feeType) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		String base = "";
		if (speedBase == 0) {
			base = "click";
		} else if (speedBase == 1) {
			base = "impressions";
		} else if (speedBase == 2) {
			if (feeType == 0) {
				base = "budget.cpc";
			} else {
				base = "budget.cpm";
			}
		}

		return new StringBuilder(CACHE_NAME).append(":").append(id).append(":").append(date).append(":")
				.append("latest.consume").append(":").append(base).toString();
	}

	@Deprecated
	public String addAmountCountKey(long lastTime) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder("strategy:").append(this.getId()).append(":").append(date).append(":latest.add:")
				.append(lastTime).toString();
	}

	public static String addAmountCountKey(long id, long lastTime) {
		String date = DateTime.now().toString("yyyy-MM-dd");
		return new StringBuilder("strategy:").append(id).append(":").append(date).append(":latest.add:")
				.append(lastTime).toString();
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Campaign getCampaign() {
		return campaign;
	}

	public void setCampaign(Campaign campaign) {
		this.campaign = campaign;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int getMaxImpressions() {
		return maxImpressions;
	}

	public void setMaxImpressions(int maxImpressions) {
		this.maxImpressions = maxImpressions;
	}

	public int getDayImpressions() {
		return dayImpressions;
	}

	public void setDayImpressions(int dayImpressions) {
		this.dayImpressions = dayImpressions;
	}

	public int getMaxClicks() {
		return maxClicks;
	}

	public void setMaxClicks(int maxClicks) {
		this.maxClicks = maxClicks;
	}

	public int getDayClicks() {
		return dayClicks;
	}

	public void setDayClicks(int dayClicks) {
		this.dayClicks = dayClicks;
	}

	public BigDecimal getBudget() {
		return budget;
	}

	public void setBudget(BigDecimal budget) {
		this.budget = budget;
	}

	public BigDecimal getDayBudget() {
		return dayBudget;
	}

	public void setDayBudget(BigDecimal dayBudget) {
		this.dayBudget = dayBudget;
	}

	public int getSpeedMode() {
		return speedMode;
	}

	public void setSpeedMode(int speedMode) {
		this.speedMode = speedMode;
	}

	public int getSpeedBase() {
		return speedBase;
	}

	public void setSpeedBase(int speedBase) {
		this.speedBase = speedBase;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getBuyMode() {
		return buyMode;
	}

	public void setBuyMode(int buyMode) {
		this.buyMode = buyMode;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public int getFeeType() {
		return feeType;
	}

	public void setFeeType(int feeType) {
		this.feeType = feeType;
	}

	public List<Interest> getInterestTargert() {
		return interestTargert;
	}

	public void setInterestTargert(List<Interest> interestTargert) {
		this.interestTargert = interestTargert;
	}

	public StrategyCrowdMap getCrowdTargert() {
		return crowdTargert;
	}

	public void setCrowdTargert(StrategyCrowdMap crowdTargert) {
		this.crowdTargert = crowdTargert;
	}

	public StrategyDeviceMap getDeviceTargert() {
		return deviceTargert;
	}

	public void setDeviceTargert(StrategyDeviceMap deviceTargert) {
		this.deviceTargert = deviceTargert;
	}

	public DayTargert getDayTargert() {
		return dayTargert;
	}

	public void setDayTargert(DayTargert dayTargert) {
		this.dayTargert = dayTargert;
	}

	public List<Integer> getGeoCountryTargert() {
		return geoCountryTargert;
	}

	public void setGeoCountryTargert(List<Integer> geoCountryTargert) {
		this.geoCountryTargert = geoCountryTargert;
	}

	public List<Integer> getGeoStateTargert() {
		return geoStateTargert;
	}

	public void setGeoStateTargert(List<Integer> geoStateTargert) {
		this.geoStateTargert = geoStateTargert;
	}

	public List<Integer> getGeoCityTargert() {
		return geoCityTargert;
	}

	public void setGeoCityTargert(List<Integer> geoCityTargert) {
		this.geoCityTargert = geoCityTargert;
	}

	public List<Integer> getGeoDistrictTargert() {
		return geoDistrictTargert;
	}

	public void setGeoDistrictTargert(List<Integer> geoDistrictTargert) {
		this.geoDistrictTargert = geoDistrictTargert;
	}

	public List<Creative> getCreatives() {
		return creatives;
	}

	public void setCreatives(List<Creative> creatives) {
		this.creatives = creatives;
	}

	public boolean equals(Object arg0) {
		if (arg0 instanceof Strategy) {
			return this.id == ((Strategy) arg0).id;
		} else {
			return super.equals(arg0);
		}
	}

	public int hashCode() {
		return Long.valueOf(id).hashCode();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public int getCampaignId() {
		return campaignId;
	}

	public void setCampaignId(int campaignId) {
		this.campaignId = campaignId;
	}

	public List<AdFormat> getFormatTypeList() {
		return formatTypeList;
	}

	public void setFormatTypeList(List<AdFormat> formatTypeList) {
		this.formatTypeList = formatTypeList;
	}

	public StrategyGroup getStrategyGroup() {
		return strategyGroup;
	}

	public void setStrategyGroup(StrategyGroup strategyGroup) {
		this.strategyGroup = strategyGroup;
	}

	public int getStrategyGroupId() {
		return strategyGroupId;
	}

	public void setStrategyGroupId(int strategyGroupId) {
		this.strategyGroupId = strategyGroupId;
	}

	public List<Creative> getMatchCreatives() {
		return matchCreatives;
	}

	public void setMatchCreatives(List<Creative> matchCreatives) {
		this.matchCreatives = matchCreatives;
	}

	public List<Long> getSpecifyCreativeIds() {
		return specifyCreativeIds;
	}

	public void setSpecifyCreativeIds(List<Long> specifyCreativeIds) {
		this.specifyCreativeIds = specifyCreativeIds;
	}

}
